#include <miscOperate.h>

int memcpypro(void *dest, size_t destsize, const void *src, size_t srcsize)
{
    size_t tmpcpysize = destsize > srcsize ? srcsize : destsize;
    if (!dest || !src || tmpcpysize < 1)
    {
        return 0;
    }
    memcpy(dest, src, tmpcpysize);
    return tmpcpysize;
}

int memcpystr(void *dest, size_t destsize, const void *src)
{
    size_t tmpcpysize = strlen(src) + 1;
    tmpcpysize = destsize > tmpcpysize ? tmpcpysize : destsize;
    if (!dest || !src || tmpcpysize < 1)
    {
        return 0;
    }
    memcpy(dest, src, tmpcpysize);
    // if(tmpcpysize == destsize)
    // {
    //     ((char *)dest)[tmpcpysize - 1] = '\0';
    // }
    return tmpcpysize;
}

int hexBufferToReadableString(const char *inbuf, int insize, char *outbuffer, int outsize, int littleCharFlag)
{
    const char *tmpConvertString;
    int i;
    if (outsize < 1)
    {
        return 0;
    }
    insize = LIMIT_VAL(insize, 0, (outsize - 1) / 2);
    if (littleCharFlag)
    {
        tmpConvertString = "0123456789abcdef";
    }
    else
    {
        tmpConvertString = "0123456789ABCDEF";
    }
    for (i = 0; i < insize; i++)
    {
        outbuffer[i * 2] = tmpConvertString[(inbuf[i] >> 4) & 0xf];
        outbuffer[i * 2 + 1] = tmpConvertString[(inbuf[i] >> 0) & 0xf];
    }
    outbuffer[insize * 2] = '\0';
    return insize * 2;
}
struct hexStrFixedBytesStruct getFixedHexBytesStr(const uint8_t *cvrtData, int cvrtSize)
{
    struct hexStrFixedBytesStruct tmpSnStr;
    hexBufferToReadableString(cvrtData, cvrtSize, tmpSnStr.byteStr, sizeof(tmpSnStr.byteStr), 0);
    return tmpSnStr;
}

void logHexData(const char *hexExplain, const uint8_t *cvrtData, int cvrtSize)
{
#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
    uint8_t MCU_XDATA_TYPE tmpdbgstr[2048];
    hexBufferToReadableString(cvrtData, cvrtSize, tmpdbgstr, sizeof(tmpdbgstr), 0);
    logdebug("[%s]%s", hexExplain, tmpdbgstr);
#else
    hexExplain = NULL;
    cvrtData = NULL;
    cvrtSize = 0;
#endif
}
